package com.vickyleu.audiorecorder

object MP3Recorder {
    /**
     * init lame
     * @param inSampleRate
     * input sample rate in Hz
     * @param channel
     * number of channels
     * @param mode
     * 0 = CBR, 1 = VBR, 2 = ABR.  default = 0
     * @param outSampleRate
     * output sample rate in Hz
     * @param outBitRate
     * rate compression ratio in KHz
     * @param quality
     * quality=0..9. 0=best (very slow). 9=worst.<br></br>
     * recommended:<br></br>
     * 2 near-best quality, not too slow<br></br>
     * 5 good quality, fast<br></br>
     * 7 ok quality, really fast
     */
    external fun init(
        inSampleRate: Int, channel: Int, mode: Int,
        outSampleRate: Int, outBitRate: Int, quality: Int
    )

    /**
     * file convert to mp3
     * it may cost a lot of time and better put it in a thread
     * @param inputPath
     * file path to be converted
     * @param mp3Path
     * mp3 output file path
     */
    external fun convertMp3(inputPath: String?, mp3Path: String?)

    /**
     * Encode buffer to mp3.
     *
     * @param bufferLeft
     * PCM data for left channel.
     * @param bufferRight
     * PCM data for right channel.
     * @param samples
     * number of samples per channel.
     * @param mp3buf
     * result encoded MP3 stream. You must specified
     * "7200 + (1.25 * buffer_l.length)" length array.
     * @return
     *
     *number of bytes output in mp3buf. Can be 0.
     *
     * -1: mp3buf was too small
     *
     * -2: malloc() problem
     *
     * -3: lame_init_params() not called
     * -4: psycho acoustic problems
     */
    external fun encode(
        bufferLeft: ShortArray?, bufferRight: ShortArray?,
        samples: Int, mp3buf: ByteArray?
    ): Int

    /**
     * Flush LAME buffer.
     *
     * REQUIRED:
     * lame_encode_flush will flush the intenal PCM buffers, padding with
     * 0's to make sure the final frame is complete, and then flush
     * the internal MP3 buffers, and thus may return a
     * final few mp3 frames.  'mp3buf' should be at least 7200 bytes long
     * to hold all possible emitted data.
     *
     * will also write id3v1 tags (if any) into the bitstream
     *
     * return code = number of bytes output to mp3buf. Can be 0
     * @param mp3buf
     * result encoded MP3 stream. You must specified at least 7200
     * bytes.
     * @return number of bytes output to mp3buf. Can be 0.
     */
    external fun flush(mp3buf: ByteArray?): Int

    /**
     * Close LAME.
     */
    external fun close()

    /**
     * get converted bytes in inputBuffer
     * @return
     * converted bytes in inputBuffer
     * to ignore the deviation of the file size,when return to -1 represents convert complete
     */
    val convertBytes: Long
        external get

    /**
     * get library lame version
     * @return
     */
    val lameVersion: String?
        external get

    init {
        System.loadLibrary("lame")
    }
}